package com.tyfspring.beans.config;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description bean属性列表
 * @Author shallow
 * @Date 2023/3/26 13:28
 */

public class PropertyValues {

    private final List<PropertyValue> propertyValueList = new ArrayList<>();

    /**
     * 添加属性
     * @param pv
     */
    public void addPropertyValue(PropertyValue pv){
        for (int i = 0; i < propertyValueList.size(); i++) {
            PropertyValue currentPv = this.propertyValueList.get(i);
            if (currentPv.getName().equals(pv.getName())) {
                //属性名相同，覆盖原有属性值
                this.propertyValueList.set(i,pv);
                return;
            }
        }
        //新属性，直接加入列表
        this.propertyValueList.add(pv);
    }

    /**
     * 输出propertyValue数组
     * @return
     */
    public PropertyValue[] getPropertyValues(){
        return this.propertyValueList.toArray(new PropertyValue[0]);
    }


    /**
     * 根据属性名获取属性值
     * @param propertyName
     * @return
     */
    public PropertyValue getPropertyValue(String propertyName) {
        for (int i = 0; i < this.propertyValueList.size(); i++) {
            PropertyValue pv = this.propertyValueList.get(i);
            if (pv.getName().equals(propertyName)) {
                return pv;
            }
        }
        return null;
    }
}
